package meterMonitor.controller;

import meterMonitor.biz.MeterToolsBiz;
import meterMonitor.consts.ResponseCode;
import meterMonitor.model.Meter;
import meterMonitor.util.BizException;
import meterMonitor.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/form1")
public class Form1Controller {

    public static final Logger logger = LoggerFactory.getLogger(Form1Controller.class);

    @Autowired
    MeterToolsBiz meterToolsBiz;

    @RequestMapping("/BtnLoadMeter_Click")
    public BaseResponseData BtnLoadMeter_Click() {
        BaseResponseData response = new BaseResponseData();
        try {
            List<MeterTools> list = meterToolsBiz.BtnLoadMeterClick();
            response.setData(list);
            response.setCode(ResponseCode.SUCCESS);
        } catch (BizException ex) {
            this.logger.error(ex.getMessage(), ex);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(ex.getMessage());
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    @RequestMapping("/updateCurrentQuantiry")
    public BaseResponseData updateCurrentQuantiry(double currentQuantiry, String deviceName) {
        BaseResponseData response = new BaseResponseData();
        try {
            int a = meterToolsBiz.updateCurrentQuantiry(currentQuantiry, deviceName);

            response.setCode(ResponseCode.SUCCESS);
            response.setMessage("修改成功！");
            response.setData(a);

        } catch (BizException ex) {
            this.logger.error(ex.getMessage(), ex);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(ex.getMessage());
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    @RequestMapping("/getMonitorToday")
    public BaseResponseData getMonitorToday(String meterCode, String uploadDate) {
        BaseResponseData response = new BaseResponseData();
        try {
            MonitorAll mod = meterToolsBiz.getMonitorToday(meterCode, uploadDate);
            response.setCode(ResponseCode.SUCCESS);
            response.setData(mod);
        } catch (BizException ex) {
            this.logger.error(ex.getMessage(), ex);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(ex.getMessage());
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    @RequestMapping("/getMonitorBef")
    public BaseResponseData getMonitorBef(String meterCode, String uploadDate, Long PKID) {
        BaseResponseData response = new BaseResponseData();
        try {
            MonitorAll list = meterToolsBiz.getMonitorBef(meterCode, uploadDate);
            response.setCode(ResponseCode.SUCCESS);
            response.setData(list);
        } catch (BizException ex) {
            this.logger.error(ex.getMessage(), ex);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(ex.getMessage());
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    @RequestMapping("/updCurrent_quantiryInterval_quantity")
    public BaseResponseData updCurrentQuantiryIntervalQuantity(double currentQuantiry, double intervalQuantity, Integer PKID) {
        BaseResponseData response = new BaseResponseData();
        try {
            int a = meterToolsBiz.updCurrentQuantiryIntervalQuantity(currentQuantiry, intervalQuantity, PKID);
            response.setCode(ResponseCode.SUCCESS);
            response.setMessage("修改成功！");
            response.setData(a);
        } catch (BizException ex) {
            this.logger.error(ex.getMessage(), ex);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(ex.getMessage());
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    @RequestMapping("/getDetailToday")
    public BaseResponseData getDetailToday(String meterCode, String uploadDate) {
        BaseResponseData response = new BaseResponseData();
        try {
            MonitorDetailAll list = meterToolsBiz.getDetailToday(meterCode, uploadDate);
            response.setCode(ResponseCode.SUCCESS);
            response.setData(list);
        } catch (BizException ex) {
            this.logger.error(ex.getMessage(), ex);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(ex.getMessage());
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    @RequestMapping("/getDetailBef")
    public BaseResponseData getDetailBef(String meterCode, Integer PKID) {
        BaseResponseData response = new BaseResponseData();
        try {
            MonitorDetailAll list = meterToolsBiz.getDetailBef(meterCode, PKID);
            response.setCode(ResponseCode.SUCCESS);
            response.setData(list);
        } catch (BizException ex) {
            this.logger.error(ex.getMessage(), ex);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(ex.getMessage());
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    @RequestMapping("/updDetailToday")
    public BaseResponseData updDetailToday(double currentQuantiry, double intervalQuantity, double receiveIntervalQuantity, Integer PKID) {
        BaseResponseData response = new BaseResponseData();
        try {
            int a = meterToolsBiz.updDetailToday(currentQuantiry, intervalQuantity, receiveIntervalQuantity, PKID);

            response.setCode(ResponseCode.SUCCESS);
            response.setMessage("修改成功！");
            response.setData(a);
        } catch (BizException ex) {
            this.logger.error(ex.getMessage(), ex);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(ex.getMessage());
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    @RequestMapping("/getHourLatest")
    public BaseResponseData getHourLatest(String meterCode, String uploadDate) {
        BaseResponseData response = new BaseResponseData();
        try {
            MonitorHourAll list = meterToolsBiz.getHourLatest(meterCode, uploadDate);
            response.setCode(ResponseCode.SUCCESS);
            response.setData(list);
        } catch (BizException ex) {
            this.logger.error(ex.getMessage(), ex);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(ex.getMessage());
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    @RequestMapping("/getHourBef")
    public BaseResponseData getHourBef(String meterCode, Integer PKID) {
        BaseResponseData response = new BaseResponseData();
        try {
            MonitorHourAll list = meterToolsBiz.getHourBef(meterCode, PKID);
            response.setCode(ResponseCode.SUCCESS);
            response.setData(list);
        } catch (BizException ex) {
            this.logger.error(ex.getMessage(), ex);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(ex.getMessage());
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    @RequestMapping("/updHour")
    public BaseResponseData updHour(double currentQuantiry, double intervalQuantity, Integer PKID) {
        BaseResponseData response = new BaseResponseData();
        try {
            int a = meterToolsBiz.updHour(currentQuantiry, intervalQuantity, PKID);
            response.setCode(ResponseCode.SUCCESS);
            response.setMessage("修改成功！");
            response.setData(a);
        } catch (BizException ex) {
            this.logger.error(ex.getMessage(), ex);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(ex.getMessage());
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    @RequestMapping("/monitorInsert")
    public BaseResponseData monitorInsert(MonitorAll monitorAll) {
        BaseResponseData response = new BaseResponseData();
        try {
            Meter mm = meterToolsBiz.getModByCode(monitorAll.getMeterCode());
            if (mm != null && mm.getStandard() != null) {
                monitorAll.setStandard(mm.getStandard());
            }
            int a = meterToolsBiz.monitorInsert(monitorAll);
            response.setCode(ResponseCode.SUCCESS);
            response.setData(monitorAll.getPKID());
        } catch (BizException ex) {
            this.logger.error(ex.getMessage(), ex);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(ex.getMessage());
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    @RequestMapping("/getMonitorHourToday")
    public BaseResponseData getMonitorHourToday(String meterCode) {
        BaseResponseData response = new BaseResponseData();
        try {
            MonitorHourAll list = meterToolsBiz.getMonitorHourToday(meterCode);
            response.setCode(ResponseCode.SUCCESS);
            response.setData(list);
        } catch (BizException ex) {
            this.logger.error(ex.getMessage(), ex);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(ex.getMessage());
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    @RequestMapping("/monitorHourInsert")
    public BaseResponseData monitorHourInsert(MonitorHourAll monitorHourAll) {
        BaseResponseData response = new BaseResponseData();
        try {
            int a = meterToolsBiz.monitorHourInsert(monitorHourAll);
            response.setCode(ResponseCode.SUCCESS);
            response.setData(monitorHourAll.getPKID());
        } catch (BizException ex) {
            this.logger.error(ex.getMessage(), ex);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(ex.getMessage());
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    @RequestMapping("/monitorDetailLatest")
    public BaseResponseData monitorDetailLatest(String meterCode) {
        BaseResponseData response = new BaseResponseData();
        try {
            MonitorDetailAll list = meterToolsBiz.monitorDetailLatest(meterCode);
            response.setCode(ResponseCode.SUCCESS);
            response.setData(list);
        } catch (BizException ex) {
            this.logger.error(ex.getMessage(), ex);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(ex.getMessage());
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    @RequestMapping("/monitorDetailInsert")
    public BaseResponseData monitorDetailInsert(MonitorDetailAll monitorDetailAll) {
        BaseResponseData response = new BaseResponseData();
        try {
            int a = meterToolsBiz.monitorDetailInsert(monitorDetailAll);
            response.setCode(ResponseCode.SUCCESS);
            response.setData(monitorDetailAll.getPKID());
        } catch (BizException ex) {
            this.logger.error(ex.getMessage(), ex);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(ex.getMessage());
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    @RequestMapping("/getMonitorByUploadDate")
    public BaseResponseData getMonitorByUploadDate(String uploadDate) {
        BaseResponseData response = new BaseResponseData();
        try {
            List<MonitorAll> list = meterToolsBiz.getMonitorByUploadDate(uploadDate);
            response.setCode(ResponseCode.SUCCESS);
            response.setData(list);
        } catch (BizException ex) {
            this.logger.error(ex.getMessage(), ex);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(ex.getMessage());
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    @RequestMapping("/getMonitorBymeterCode")
    public BaseResponseData getMonitorBymeterCode(String meterCode) {
        BaseResponseData response = new BaseResponseData();
        try {
            List<MonitorAll> list = meterToolsBiz.getMonitorBymeterCode(meterCode);
            response.setCode(ResponseCode.SUCCESS);
            response.setData(list);
        } catch (BizException ex) {
            this.logger.error(ex.getMessage(), ex);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(ex.getMessage());
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    @RequestMapping("/getMeterStatusAll")
    public BaseResponseData getMeterStatusAll(String uploadDate) {
        BaseResponseData response = new BaseResponseData();
        try {
            List<MeterStatus> list = meterToolsBiz.getMeterStatusAll(uploadDate);
            response.setCode(ResponseCode.SUCCESS);
            response.setData(list);
        } catch (BizException ex) {
            this.logger.error(ex.getMessage(), ex);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(ex.getMessage());
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            response.setCode(ResponseCode.BUSEILL_EXCEPTION);
            response.setMessage(e.getMessage());
        }
        return response;
    }

}
